package com.qyt.hotel.controller;

import com.qyt.hotel.config.GlobalProperties;
import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.RoomType;
import com.qyt.hotel.entity.RoomTypePrice;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/roomType")
public class RoomTypeController {

    @Resource
    private RoomTypeService roomTypeService;

    @Autowired
    private GlobalProperties globalProperties;

    @GetMapping("/getOne")
    public Result getOne(RoomType roomType) {
        RoomType roomType1 = roomTypeService.getOne(roomType);
        if (null != roomType1) {
            return new Result("查询成功", true, StatusCode.OK, roomType1);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @GetMapping("/list")
    public Result getRoomTypeList() {
        List<RoomType> roomTypeList = roomTypeService.getRoomTypeList();
        return new Result("查询成功", true, StatusCode.OK, roomTypeList);
    }

    @PostMapping("/uploadImg")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        // 获得原始文件名+格式
        String OriginalFileName = file.getOriginalFilename();
        if (null != OriginalFileName) {
            // 截取文件名
            String fileName = OriginalFileName.substring(0, OriginalFileName.lastIndexOf("."));
            // 截取文件格式
            String fileType = OriginalFileName.substring(OriginalFileName.lastIndexOf("."));
            //获取当前时间(精确到毫秒)
            Date date = new Date();
            String formatDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
            //原文件名+当前时间作为新文件名
            String fileNewName = formatDate + (new Random()).nextInt(10000) + fileType;
            File f = new File(globalProperties.getRoomTypeStaticPath());
            if (!f.exists()) {
                boolean mkdirs = f.mkdirs();
            }
            if (!file.isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(globalProperties.getRoomTypeStaticPath() + fileNewName);
                    InputStream in = file.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = in.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result("上传异常", false, null, null);
                }
            }
            return new Result("上传成功", true, StatusCode.OK, fileNewName);
        } else {
            return new Result("上传失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/add")
    public Result addAdmin(RoomType roomType) {
        Integer count = roomTypeService.addRoomType(roomType);
        if (null != count) {
            return new Result("添加成功", true, StatusCode.OK, null);
        } else {
            return new Result<>("添加失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/delete")
    public Result deleteRoomType(RoomType roomType) {
        Integer count = roomTypeService.deleteRoomType(roomType);
        if (null != count) {
            return new Result("删除成功", true, StatusCode.OK, null);
        } else {
            return new Result("该房间类型已被使用", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/update")
    public Result updateAdmin(RoomType roomType) {
        Integer count = roomTypeService.updateRoomType(roomType);
        if (null != count) {
            return new Result("修改成功", true, StatusCode.OK, null);
        } else {
            return new Result("修改失败", false, StatusCode.ERROR, null);
        }
    }

    @GetMapping("/totalTypePrice")
    public Result statisticsRoomTypePrice() {
        List<RoomTypePrice> roomTypePriceList = roomTypeService.statisticsRoomTypePrice();
        return new Result("查询成功", true, StatusCode.OK, roomTypePriceList);
    }


}